const MYSQL = require("../../sql/mysql");
const MYSQLConfig = require("../../sql/database");
const SQLName = MYSQLConfig.datasheetName.user_token;
/**
 * 删除某个token
 * @param {object} params
 * @param {string} params.user
 * @param {string} params.token
 * @param {string} params.id
 * @return {Promise<{code:number,msg:string} | {code:number,msg:string}>}
 */
module.exports = async (params = {}) => {
  try {
    let { user, token, id } = params;
    let query = `DELETE FROM ${SQLName} WHERE user = ${user}`;
    if (!user) {
      throw { code: 500, msg: "user不能为空" }
    }
    if (token) {
      query += ` AND token = '${token}'`;
    } else if (id) {
      query += ` AND id = '${id}'`;
    }
    await MYSQL.queryPromise(query);
    return { code: 0, msg: "操作成功" };
  } catch (error) {
    console.log(error);
    throw error
  }
};
